package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.ctr.DbKeywordController;
import com.hctforgreen.greenservice.model.KeywordListEntity.KeywordEntity;
import com.hctforgreen.greenservice.service.HctApplication;
import com.hctforgreen.greenservice.utils.VideoSearchStoreUtil;

/**
 * 资料详情页的搜索历史Lv
 * 
 * @author Tallen Zhai
 * 
 */
public class SearchHistoryListAdapter extends BaseAdapter {

	private Activity mActivity;
	private ListView mListView;

	private static List<KeywordEntity> mEntities;

	private int type;// 类型 0：搜索资料内容 1：搜索视频

	public SearchHistoryListAdapter(Activity activity, ListView listView,
			List<KeywordEntity> entities, int type) {

		mActivity = activity;
		mListView = listView;

		mEntities = entities;

		mListView.setDivider(mActivity.getResources().getDrawable(
				R.drawable.ic_driver));

		mListView.setAdapter(this);
		this.type = type;
		initClearHistoryBtn();
	}

	private void initClearHistoryBtn() {
		final View clearHistoryView = mActivity
				.findViewById(R.id.lyt_clear_search_history);
		final View historyView = mActivity
				.findViewById(R.id.lyt_search_history);
		clearHistoryView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (type) {
				case 0:
					HctApplication application = (HctApplication) mActivity
							.getApplication();
					DbKeywordController dbKeywordController = new DbKeywordController(
							application.getDbKeywordAdapter());
					for (KeywordEntity entity : mEntities) {
						dbKeywordController.delete(entity);
					}
					break;
				case 1:
					VideoSearchStoreUtil.save(mActivity, "");
					break;

				}

				mEntities.clear();
				notifyDataSetChanged();
				historyView.setVisibility(View.GONE);
			}
		});

	}

	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final KeywordEntity entity = (KeywordEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_search_history_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}
		viewCache.getKeywordTv().setText(entity.keyword);
		return convertView;
	}

	class ViewCache {
		private View baseView;

		private TextView keywordTv;

		public ViewCache(View view) {
			baseView = view;
		}

		public TextView getKeywordTv() {
			if (keywordTv == null) {
				keywordTv = (TextView) baseView
						.findViewById(R.id.tv_search_keyword);
			}
			return keywordTv;
		}

	}

}
